package com.saver.modules.sys.service;

import com.saver.modules.sys.dao.SysUserRoleDao;
import com.saver.modules.sys.entity.SysUserRoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;


/**
 * 用户与角色对应关系
 *
 * @author yuxiaolong
 * @email 1005471232@qq.com
 * @date 2018年08月30日 下午10:45
 */
@Service("sysUserRoleService")
public class SysUserRoleService {
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Transactional
    public void saveUserRoleSet(Set<Long> set, Long userId) {
        deleteByUserId(userId);
        set.forEach(item -> {
            SysUserRoleEntity entity = new SysUserRoleEntity();
            entity.setUserId(userId);
            entity.setRoleId(item);
            sysUserRoleDao.save(entity);
        });
    }

    public Integer deleteByUserId(Long userId) {
        return sysUserRoleDao.deleteByUserId(userId);
    }

}
